package com.emou.reflection;

import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Hashtable;

/**
 * Created by Emou on 2021/1/14
 */
public class ReflectionDemo {
    public void m1(){

    }
    public void getNameTest(){
        String name = "emou";
        Class clazz = name.getClass();
        System.out.println(clazz.getName());
        Button button = new Button();
        Class clazzButton = button.getClass();
        System.out.println(clazzButton.getName());
        Class clazzButtonSuperclass = clazzButton.getSuperclass();
        System.out.println(clazzButtonSuperclass.getName());

    }

    public String testMethod(String str, Hashtable hashtable) {
        String returnStr = "要返回的数据";
        System.out.println("获取到map数据  " + hashtable.get("name"));
        return returnStr;
    }
    public void getMethod() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class cls = Class.forName("com.emou.reflection.ReflectionDemo");
        Class[] ptypes = new Class[2];
        ptypes[0] = Class.forName("java.lang.String");
        ptypes[1] = Class.forName("java.util.Hashtable");
        Method method = cls.getMethod("testMethod", ptypes);
        Object[] args = new Object[2];
        args[0] = "aaa";
        Hashtable hashtable = new Hashtable<>();
        hashtable.put("name", "gibson");
        args[1] = hashtable;
        String str = (String) method.invoke(new ReflectionDemo(), args);
        System.out.println(str);
    }

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        ReflectionDemo reflectionDemo = new ReflectionDemo();
//        reflectionDemo.getNameTest();
        reflectionDemo.getMethod();
    }
}
